<script setup>
import {getCurrentInstance, inject, ref} from "vue";
import {ElMessage} from "element-plus";

const customer_name = ref("")
const customer_phone = ref("")
const customer_address = ref("")
const visible = ref(false);
const {proxy} = getCurrentInstance();
const imageUrl = ref('');
const formData = new FormData();
const dialogData = inject('modifyDialog');

const open = () => {
  customer_name.value = dialogData.value[0];
  customer_phone.value = dialogData.value[1];
  customer_address.value = dialogData.value[2];
  visible.value = true;
}

const emits = defineEmits(['close'])

const close = () => {
  visible.value = false
  emits('close', false);
}

defineExpose({
  open
})

const submitCustomer = () => {
  proxy.$axios.put("http://localhost:8091/TbCustomer/updateCustomer",
    {id: dialogData.value[3], custName:customer_name.value, custMobile:customer_phone.value, custAddress:customer_address.value}
  ).then(resp => {
    if (resp.data > 0) {
      ElMessage.success("操作成功");
    } else {
      ElMessage.error("操作失败");
    }
    visible.value = false;
  })
}

const beforeAvatarUpload = (rawFile) => {
  if (rawFile.type !== 'image/jpeg') {
    ElMessage.error('Avatar picture must be JPG format!');
    return false;
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('Avatar picture size can not exceed 2MB!');
    return false;
  }
  imageUrl.value = URL.createObjectURL(rawFile);
  formData.append('multipartFile', rawFile)
  return true;
};
</script>

<template>
  <el-dialog v-model="visible" @close="close" width="20%">
    <div id="formStyle">
      <el-form>
        <el-form-item><span>姓名</span>
          <el-input v-model="customer_name"></el-input>
        </el-form-item>
        <el-form-item><span>地址</span>
          <el-input v-model="customer_address"></el-input>
        </el-form-item>
        <el-form-item><span>电话</span>
          <el-input v-model="customer_phone"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitCustomer">提交</el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-dialog>
</template>

<style scoped lang="scss">
.el-dialog {
  .el-input {
    margin-left: 20px;
    width: 70%;
  }

  #formStyle {
    display: flex;
    flex-direction: column;
    align-items: center;
  }

  .avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
  }

  .avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
  }

  :deep(.el-icon .avatar-uploader-icon) {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
  }
}

</style>